using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NewLife;

namespace UdpServerTest
{
    public partial class Form1 : Form
    {
        private string ipstr;
        private int portstr;

        private string ripstr;
        private int rportstr;

        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                label5.Text = "等待连接";
                ipstr = ip.Text;
                portstr = int.Parse(port.Text);

                ripstr = rip.Text;
                rportstr = int.Parse(rport.Text);

                startServer();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }


        }

        private UdpServer udpServer;

        void startServer()
        {
            if (udpServer!=null)
            {
                udpServer.Ns.Stop("");
            }

            udpServer = new UdpServer(ipstr, portstr);

            udpServer.OnClientConnect += UdpServer_OnClientConnect;
            udpServer.OnReciveData += UdpServer_OnReciveData;

            bool stats = udpServer.Start();

            if (stats)
            {
                 addLog("已启动");
            }

        }

        private void UdpServer_OnReciveData(string ip, int port, byte[] data)
        {
            string send = $"接收{DateTime.Now}:     {data.ToHex(" ")}\r";
            if (ip == ripstr && port == rportstr)
            {
                richTextBox1.Text = richTextBox1.Text.Insert(0, send);
            }
            addLog(send);
        }


        void addLog(string str)
        {
           log.Text=  log.Text.Insert(0, DateTime.Now + str + "\r");
        }

        private void UdpServer_OnClientConnect(string ip, int port)
        {
            if (ip == ripstr && port == rportstr)
            {
                label5.Text = $"{ip}:{port} 已连接";
                button3.Enabled = true;
            }
            addLog($"{ip}:{port} 已连接");

        }

        private void button2_Click(object sender, EventArgs e)
        {
             
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var s = textBox5.Text;
            var cmd = ByteHelper.HexStringToByteArray(s);
            if (checkBox1.Checked)
            {
                cmd = ByteHelper.AddModbusCrc16(cmd);
            }

            udpServer.Send(cmd,ripstr,  rportstr);

            string send = $"发送{DateTime.Now}:     {cmd.ToHex(" ")}\r";

            richTextBox1.Text = richTextBox1.Text.Insert(0, send);

        }

        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
